package paper.bayes;

import java.util.Hashtable;
import java.util.HashMap;
import java.util.Enumeration;
import java.io.*;

public class PDF {
    private Hashtable<String,Double> pdf;
    private long n;
    public final long minElements = 2;
    private static HashMap<File,PDF> allPDFs = new HashMap<File,PDF>();

    public PDF() {
        pdf = new Hashtable<String,Double>();
        n = 0;
    }

    public PDF(File f)  throws IOException, FileNotFoundException  {
        PDF x = allPDFs.get(f);
        if (x != null) {
            pdf = x.pdf;
        } else {
            load(f);
            allPDFs.put(f,this);
        }
    }

    private void load(File f) throws IOException, FileNotFoundException {
        try {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
            pdf = (Hashtable<String,Double>)ois.readObject();
            ois.close();
        } catch (ClassNotFoundException e) {
            System.err.println("Shit " + e);
        }
    }

    public void save(File f) throws IOException, FileNotFoundException  {
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pdf);
        oos.close();
    }

    public void build(Reader r) throws FileNotFoundException{
        try {
            BufferedReader br = new BufferedReader(r);
            String line;
            while ((line = br.readLine()) != null) {
                add(line);
            }
            br.close();
        } catch (IOException e) {
            System.err.println("Shit "+ e);
        }
        done();
    }

    public void done() {
        Enumeration<String> keys = pdf.keys();
        while (keys.hasMoreElements() ) {
            String k = keys.nextElement();
            Double d = pdf.get(k);
            if (d < minElements) {
                pdf.remove(k);
                n--;
            }
        }
        keys = pdf.keys();
        while (keys.hasMoreElements() ) {
            String k = keys.nextElement();
            Double d = pdf.get(k);
            pdf.put(k, new Double(d/n));
        }
        
    }

    public void add(String line) {
        String words[] = line.toLowerCase().replaceAll("[^-a-z&;' ]+","").split(" ");
        for (int i = 0; i < words.length; i++) {
            if (!words[i].equals("")) {
                Double d = pdf.get(words[i]);
                if (d == null) {
                    d = new Double(0);
                }
                pdf.put(words[i], new Double(d+1));
                n++;
            }
        }
    }

    Double get(String x) {
        return pdf.get(x);
    }

    public String toString() {
        StringBuilder x = new StringBuilder();
        Enumeration<String> keys = pdf.keys();
        while (keys.hasMoreElements() ) {
            String k = keys.nextElement();
            Double d = pdf.get(k);
            x.append("" + d + " " + k + "\n");
        }
        return x.toString();
    }

    public static void main(String[] args) throws IOException {
        PDF x = new PDF();
        x.load(new File(args[0]));
        System.out.println(x.toString());
    }

}
